﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Message_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/Message_Edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public IUser_messageService _user_message { get; set; }
        public IUsersService _users { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_message", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            this.action = Request.Query["action"];
            if (!string.IsNullOrEmpty(action) && action == CMSEnums.ActionEnum.View.ToString())
            {
                this.action = CMSEnums.ActionEnum.View.ToString();//修改类型
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    ViewBag.ClientScript = JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_user_message.Exists(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }
        // GET: /admin/Message_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.View.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                ViewData["model"] = new User_message();
                ViewBag.div_view_visible = "none";
                ViewBag.div_add_visible = "normal";
                ViewBag.btnSubmit_visible = "normal";
            }
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ChkAdminLevel("user_message", CMSEnums.ActionEnum.Add.ToString()); //检查权限
            if (!DoAdd())
            {
                ViewBag.ClientScript = JscriptMsg("发送过程中发生错误！", "back");
            }
            else
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "发送站内短消息"); //记录日志
                ViewBag.ClientScript = JscriptMsg("发送短消息成功", "../message_list/index");
            }
            return View(EDIT_RESULT_VIEW);
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _user_message.GetModel(_id);
            ViewData["model"] = model;
            ViewBag.div_view_visible = "normal";
            ViewBag.div_add_visible = "none";
            ViewBag.btnSubmit_visible = "none";
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = true;
            User_message model = new User_message();

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.content = Request.Form["txtContent"];

            string[] arrUserName = Request.Form["txtUserName"].FirstOrDefault().Trim().Split(',');
            if (arrUserName.Length > 0)
            {
                foreach (string username in arrUserName)
                {
                    if (_users.Exists(username))
                    {
                        model.accept_user_name = username;
                        if (_user_message.Add(model) < 1)
                        {
                            result = false;
                        }
                    }
                }
            }

            return result;
        }
        #endregion
    }
}
